﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI.Editor.Diff;
using System.Net;

namespace Medianamik.UI.ContentManagement
{
    public partial class Diff : System.Web.UI.Page
    {
        protected string UrlParam
        {
            get
            {
                return Server.UrlDecode(Request["url"]);  
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                HttpWebResponse original = (HttpWebResponse) WebRequest.Create(UrlParam).GetResponse();
                HttpWebResponse modified = (HttpWebResponse) WebRequest.Create(UrlParam + "?mkg=-1").GetResponse();

                StreamReader originalReader = new StreamReader(original.GetResponseStream());
                StreamReader modifiedReader = new StreamReader(modified.GetResponseStream());



                DiffEngine diff = new DiffEngine();
                string diffedHtml = diff.GetDiffs(modifiedReader.ReadToEnd(), originalReader.ReadToEnd());

                //Ajouter les styles spéciaux
                string targetTag = "<head>";
                string styles =
                    @"
<style type=""text/css"">
.diff_new {background-color:#00ff00;}
.diff_deleted {color:red;text-decoration:line-through;}
</style>
";
                diffedHtml = Regex.Replace(diffedHtml, "<head.*?>", targetTag + styles, RegexOptions.IgnoreCase);

                Response.Write(diffedHtml);
            }
            catch(WebException exc)
            {
                Response.Write(new StreamReader(exc.Response.GetResponseStream()).ReadToEnd() );
            }

        }
    }
}
